package com.sxbbc.api.controller;

import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxbbc.common.core.entity.jz.*;
import com.sxbbc.common.core.service.ISysNewsService;
import com.sxbbc.common.core.service.system.login.LoginTokenService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.api.common.HomeMapping;
import com.sxbbc.api.service.IHomeService;
import com.sxbbc.common.basics.service.solr.entity.Solrpage;
import com.sxbbc.common.core.dto.jz.CityAddressVo;
import com.sxbbc.common.core.service.IAppHomeRecommendService;
import com.sxbbc.common.core.service.system.solr.entity.SolrProductDto;
import com.sxbbc.common.core.utils.BasePage;

/**
 * 首页显示 控制层
 *
 * @author leehao
 * @date 2019-05-15 14:21
 */
@RestController
public class HomeController {

    @Autowired
    private IHomeService homeService;
    @Autowired
    private IAppHomeRecommendService recommendService;
    @Autowired
    private ISysNewsService sysNewsService;
    @Autowired
    private LoginTokenService loginTokenService;

    /**
     * 首页 banner图
     *
     * @author leehao
     */
    @GetMapping(value = HomeMapping.SHOW_HOME_BANNER)
    public RestResponse showHomeBanner() {
        List<AppBanner> list = homeService.queryBannerList();
        return GetRest.getSuccess("success", list);
    }

    /**
     * 首页-导航图标
     *
     * @author leehao
     */
    @GetMapping(value = HomeMapping.SHOW_HOME_NAV_ICON)
    public RestResponse showHomeNavIcon() {
        List<AppIcon> list = homeService.queryIconList();
        return GetRest.getSuccess("success", list);
    }

    /**
     * 首页-自定义分类图标
     *
     * @author leehao
     */
    @GetMapping(value = HomeMapping.SHOW_HOME_CLASSIFY_ICON)
    public RestResponse showHomeClassifyIcon() {
        List<AppClassification> list = homeService.queryClassificationList();
        return GetRest.getSuccess("success",list);
    }

    /**
     * 首页-消息公告
     * 显示新闻中心
     * @author leehao
     */
    @GetMapping(value = HomeMapping.SHOW_HOME_MESSAGE)
    public RestResponse showHomeMessage(BasePage<SysNews> page) {
        Page<SysNews> pageInfo = sysNewsService.querySysNewsList(page);
        return GetRest.getSuccess("success",pageInfo);
    }

    /**
     * 首页-爆款推荐
     *
     * @author leehao
     */
    @GetMapping(value = HomeMapping.SHOW_HOME_RECOMMEND)
    public RestResponse showHomeRecommend(BasePage<AppHomeRecommend> page) {
        IPage<AppHomeRecommend> pageInfo = recommendService.queryRecommendList(page,new AppHomeRecommend());
        return GetRest.getSuccess("success",pageInfo);
    }

    /**
     * 首页-每日必逛
     *
     * @author leehao
     */
    @GetMapping(value = HomeMapping.SHOW_HOME_DAY)
    public RestResponse showHomeDay() {
        //先暂时写死
        List<ActivityDaily> list = homeService.queryHomeDayList();
        return GetRest.getSuccess("success",list);
    }

    /**
     * 首页商品列表
     *
     * @author leehao
     */
    @GetMapping(value = HomeMapping.SHOW_HOME_PRODUCT)
    public RestResponse showHomeProduct(String token,Integer cpage,Integer sortType,String sortDesc) {
        //默认第一页
        if (cpage == null) {
            cpage = 1;
        }
        //如果排序类型与排序方式为空，默认按照销售量升序排序
        if (sortType == null) {
            sortType = 2;
        }
        if (StringUtils.isBlank(sortDesc)) {
            sortDesc = "asc";
        }
        Integer cid = null;
        if (StringUtils.isNotBlank(token)) {
            cid = loginTokenService.validLoginCid(token);
        }
        Solrpage<SolrProductDto> pageInfo = homeService.queryHomeProductList(cid,cpage,sortType,sortDesc);
        return GetRest.getSuccess("success",pageInfo);
    }
    
    /**
     * 查询用于定位的所有城市
     * @return
     */
    @RequestMapping(value = HomeMapping.QUERY_ALL_CITY)
    public RestResponse queryAllCity () {
    	List<CityAddressVo> list = homeService.queryAllCity();
    	return GetRest.getSuccess("", list);
    }
}
